import java.awt.*;
import java.awt.event.*;
import java.util.HashSet;
import java.lang.Object.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.Timer;

public class GUI extends JPanel {
    private JButton button1;
    private JButton button2;
    private JButton button3;
    private JButton button4;
    private JButton button5;
    private JButton button6;
    private JButton button7;
    private JButton button8;
    private JButton button9;
    private JTextField display;
    private JTextField score;
    private JTextArea words;
    private JButton summit;
    private JLabel wordsdiplay;
    private JLabel scoreString;
    private ActionListener _listener;
    private Game _theGame;
    private boolean _inGame;
    private Timer timer;

    public GUI(Game theGame) {
       
       _theGame = theGame;
       
       _listener = new DigitButtonListener();
       
        //construct components
        button1 = new JButton ("");
        button1.addActionListener(_listener);
        button2 = new JButton ("");
        button2.addActionListener(_listener);
        button3 = new JButton ("");
        button3.addActionListener(_listener);
        button4 = new JButton ("");
        button4.addActionListener(_listener);
        button5 = new JButton ("");
        button5.addActionListener(_listener);
        button6 = new JButton ("");
        button6.addActionListener(_listener);
        button7 = new JButton ("");
        button7.addActionListener(_listener);
        button8 = new JButton ("");
        button8.addActionListener(_listener);
        button9 = new JButton ("");
        button9.addActionListener(_listener);
        display = new JTextField (5);
        score = new JTextField (5);
        words = new JTextArea (5, 5);
        summit = new JButton ("play");
        wordsdiplay = new JLabel ("words");
        scoreString = new JLabel ("Score");

        //adjust size and set layout
        setPreferredSize (new Dimension (601, 382));
        setLayout (null);

        _inGame = false;
        
        timer = new Timer(1000, new timerListener());
        summit.addActionListener(new EnterButtonListener());
        
        
        
        //add components
        add (button1);
        add (button2);
        add (button3);
        add (button4);
        add (button5);
        add (button6);
        add (button7);
        add (button8);
        add (button9);
        add (display);
        add (score);
        add (words);
        add (summit);
        add (wordsdiplay);
        add (scoreString);

        //set component bounds (only needed by Absolute Positioning)
        button1.setBounds (55, 105, 40, 30);
        button2.setBounds (120, 105, 40, 30);
        button3.setBounds (185, 105, 40, 30);
        button4.setBounds (55, 155, 40, 30);
        button5.setBounds (120, 155, 40, 30);
        button6.setBounds (185, 155, 40, 30);
        button7.setBounds (55, 205, 40, 30);
        button8.setBounds (120, 205, 40, 30);
        button9.setBounds (185, 205, 40, 30);
        display.setBounds (55, 50, 170, 40);
        score.setBounds (355, 20, 190, 65);
        words.setBounds (355, 100, 185, 175);
        summit.setBounds (90, 255, 100, 25);
        wordsdiplay.setBounds (310, 105, 100, 25);
        scoreString.setBounds (315, 40, 100, 25);
        //timer.start();
    }
    
    public class DigitButtonListener implements ActionListener {

       public void actionPerformed(ActionEvent e) {
          JButton source = (JButton)e.getSource();
          String label = source.getText();
          
          if (_inGame && (label.equals(".") || display.getText().indexOf(".") == -1))
             display.setText(display.getText() + label);
       }

    }
    
    public class EnterButtonListener implements ActionListener {

       //private Timer _timer;
       //private boolean _inGame;
       
//       public EnterButtonListener(Timer timer, boolean inGame)
//       {
//           //_timer = timer;
//           
//       }
       public void actionPerformed(ActionEvent e) {
          //timer.start();
          
          if(_inGame && _theGame.getCurrentRound() <= 3)
          {  
             if(_theGame.getCurrentPlayer().makeMove(display.getText()))
             {
                
                words.setText(words.getText() + "\n"+ display.getText());
                display.setText("");
                
                //score.setText(_theGame.getCurrentPlayer().getScore() + " " + _theGame.getStartTime());
             }
          }
          else
          {
             timer.start();
             _inGame = true;
             summit.setText("enter");
             words.setText("Player " + _theGame.getCurrentPlayer().getPlayer());
             if(_theGame.getCurrentPlayer().getPlayer() == 1)
             {
                button1.setText(_theGame.getBoard().getTileString()[0][0]);
                button2.setText(_theGame.getBoard().getTileString()[0][1]);
                button3.setText(_theGame.getBoard().getTileString()[0][2]);
                button4.setText(_theGame.getBoard().getTileString()[1][0]);
                button5.setText(_theGame.getBoard().getTileString()[1][1]);
                button6.setText(_theGame.getBoard().getTileString()[1][2]);
                button7.setText(_theGame.getBoard().getTileString()[2][0]);
                button8.setText(_theGame.getBoard().getTileString()[2][1]);
                button9.setText(_theGame.getBoard().getTileString()[2][2]);
             }
             
          }
          
       }

    }
    
    public class timerListener implements ActionListener 
    {
       private int _clock;
       
       public timerListener()
       {
          _clock = 10;
       }
       
       public void actionPerformed(ActionEvent e) 
       {
          if(_inGame)
          {
             if(_clock > 0)
             {
                _clock = _clock - 1;
                score.setText("score: " + _theGame.getCurrentPlayer().getScore() + "\n " + _clock + " seconds");
             }
             else
             {
                _clock = 10;
                _inGame = false;
                summit.setText("play");
                timer.stop();
                
                if(_theGame.getCurrentPlayer().getPlayer() == 1)
                {
                   summit.setText("play");
                   _theGame.switchPlayer();
                   words.setText("Player " + _theGame.getCurrentPlayer().getPlayer());
                   
                }
                else if(_theGame.getCurrentRound() == 3)
                {
                   
                   if(_theGame.getPlayer1().getScore() > _theGame.getPlayer2().getScore())
                   {
                      score.setText("Winner is Player 1");
                   }
                   else if(_theGame.getPlayer1().getScore() < _theGame.getPlayer2().getScore())
                   {
                      score.setText("Winner is Player 2");
                   }
                   else
                   {
                      score.setText("Its a tie");
                   }
                   //score.setText("Winner is _theGame.getCurrentPlayer().getScore() + "\n " + _clock + " seconds");
                }
                else
                {
                   summit.setText("play");
                   _theGame.startNextRound();
                   _theGame.switchPlayer();
                   words.setText("Player " + _theGame.getCurrentPlayer().getPlayer());
                }
             }
          }
       }

    }



    public static void main (String[] args) {
        JFrame frame = new JFrame ("MyPanel");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        String[][] test = {{"1","2","3"},{"4","5","6"},{"7","8", "A"}};
        Game theGame = new Game();
        frame.getContentPane().add (new GUI(theGame));
        frame.pack();
        frame.setVisible (true);
    }
}


